<!DOCTYPE html>
<html lang="zh_cn">
<head>
          <title>来玩魔王的咚</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta charset="utf-8" />
        <!-- twitter card metadata -->
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="/images/mowang.png">
<meta name="twitter:site" content="">
<meta name="twitter:title" content="反转 UTF-8 编码的字符串">
<meta name="twitter:description" content="<p>反转UTF-8字符串：简单的实现，原地反转，高效的原地反转；测试验证：功能测试，随机测试，基准测试</p>">
        <!-- OG Tags -->
<meta property="og:url" content="/gopl-reverse-utf8.html"/>
<meta property="og:title" content="来玩魔王的咚 | 反转 UTF-8 编码的字符串" />
<meta property="og:description" content="<p>反转UTF-8字符串：简单的实现，原地反转，高效的原地反转；测试验证：功能测试，随机测试，基准测试</p>" />
        <!-- favicon -->
        <link rel="icon" type="image/png" href="/images/mowang.png">
        <!-- moment.js for date formatting -->
        <script src="/theme/js/moment.js"></script>
        <!-- css -->
        <link rel="stylesheet" type="text/css" href="/theme/css/main.css" />
        <!-- 左边的menu，如果页面高度不够，就跟着滚动，否则文章分类显示不全 -->
        <link rel="stylesheet" type="text/css" href="/theme/css/mycss/menu.css" />
		<script>
			
                /*! grunt-grunticon Stylesheet Loader - v2.1.2 | https://github.com/filamentgroup/grunticon | (c) 2015 Scott Jehl, Filament Group, Inc. | MIT license. */
    
    (function(e){function t(t,n,r,o){"use strict";function a(){for(var e,n=0;u.length>n;n++)u[n].href&&u[n].href.indexOf(t)>-1&&(e=!0);e?i.media=r||"all":setTimeout(a)}var i=e.document.createElement("link"),l=n||e.document.getElementsByTagName("script")[0],u=e.document.styleSheets;return i.rel="stylesheet",i.href=t,i.media="only x",i.onload=o||null,l.parentNode.insertBefore(i,l),a(),i}var n=function(r,o){"use strict";if(r&&3===r.length){var a=e.navigator,i=e.Image,l=!(!document.createElementNS||!document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||e.opera&&-1===a.userAgent.indexOf("Chrome")||-1!==a.userAgent.indexOf("Series40")),u=new i;u.onerror=function(){n.method="png",n.href=r[2],t(r[2])},u.onload=function(){var e=1===u.width&&1===u.height,a=r[e&&l?0:e?1:2];n.method=e&&l?"svg":e?"datapng":"png",n.href=a,t(a,null,null,o)},u.src="",document.documentElement.className+=" grunticon"}};n.loadCSS=t,e.grunticon=n})(this);(function(e,t){"use strict";var n=t.document,r="grunticon:",o=function(e){if(n.attachEvent?"complete"===n.readyState:"loading"!==n.readyState)e();else{var t=!1;n.addEventListener("readystatechange",function(){t||(t=!0,e())},!1)}},a=function(e){return t.document.querySelector('link[href$="'+e+'"]')},c=function(e){var t,n,o,a,c,i,u={};if(t=e.sheet,!t)return u;n=t.cssRules?t.cssRules:t.rules;for(var l=0;n.length>l;l++)o=n[l].cssText,a=r+n[l].selectorText,c=o.split(");")[0].match(/US\-ASCII\,([^"']+)/),c&&c[1]&&(i=decodeURIComponent(c[1]),u[a]=i);return u},i=function(e){var t,o,a;o="data-grunticon-embed";for(var c in e)if(a=c.slice(r.length),t=n.querySelectorAll(a+"["+o+"]"),t.length)for(var i=0;t.length>i;i++)t[i].innerHTML=e[c],t[i].style.backgroundImage="none",t[i].removeAttribute(o);return t},u=function(t){"svg"===e.method&&o(function(){i(c(a(e.href))),"function"==typeof t&&t()})};e.embedIcons=i,e.getCSS=a,e.getIcons=c,e.ready=o,e.svgLoadedCallback=u,e.embedSVG=u})(grunticon,this);
                
                grunticon(["/theme/css/icons.data.svg.css", "/theme/css/icons.data.png.css", "/theme/css/icons.fallback.css"]);
            </script>
        <noscript><link href="/theme/css/icons.fallback.css" rel="stylesheet"></noscript>
        <!-- menu toggle javascript -->
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded", initMenu);
            
            function initMenu(){
                var menu = document.getElementById("menu");
                var menulink = document.getElementById("menu-link");
                menulink.addEventListener("click", function toggleMenu(){
                        window.event.preventDefault();
                        menulink.classList.toggle('active');
                        menu.classList.toggle('active');              
                    });
            };
        </script>
        <!-- 不蒜子 -->
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <meta name="description" content="<p>反转UTF-8字符串：简单的实现，原地反转，高效的原地反转；测试验证：功能测试，随机测试，基准测试</p>" />

    <meta name="tags" content="gopl" />
  <!-- 替换部分base的样式，看文章时，要再宽一点，右边有很多空间可以撑开 -->
  <link rel="stylesheet" type="text/css" href="/theme/css/mycss/article.css" />

</head>
<body>
    <div role="banner" id="masthead">
        <header>
            <a href="/"><img src="/images/mowang.png" alt="McManus Logo"></a>
                <h1>来玩魔王的咚@骑士救兵</h1>
            <a href="#menu" id="menu-link">more stuff</a>
            <nav id="menu">
                <ul>
                        <li><a href="/tags">tags</a></li>
                            <li><a href="/category/cloud.html">Cloud</a></li>
                            <li><a href="/category/docker.html">Docker</a></li>
                            <li class="active"><a href="/category/go.html">Go</a></li>
                            <li><a href="/category/linux.html">Linux</a></li>
                            <li><a href="/category/python.html">Python</a></li>
                            <li><a href="/category/xue-xi-bi-ji.html">学习笔记</a></li>
                            <li><a href="/category/yun-wei-zi-dong-hua.html">运维自动化</a></li>
                </ul>
            </nav>
        </header>
    </div>
        <div class="page" role="main">
  <div class="article" role="article">
    <article>
        <footer>
            <a name="top"></a>
            <p>
              <time datetime=" 2020-07-15 15:00:00+08:00">
                <script>document.write(moment('2020-07-15 15:00:00+08:00').format('LL'));</script>
              </time>
              ~
              <time datetime=" 2020-07-15 15:00:00+08:00">
                <script>document.write(moment('2020-07-15 15:00:00+08:00').format('LL'));</script>
              </time>
            </p>
        </footer>
        <header>
          <h2>
            反转 UTF-8 编码的字符串
          </h2>
        </header>
      <div class="content">
         <div class="toc">
<ul>
<li><a href="#fan-zhuan-utf-8-bian-ma-de-zi-fu-chuan">反转 UTF-8 编码的字符串</a><ul>
<li><a href="#jian-dan-de-shi-xian">简单的实现</a></li>
<li><a href="#yuan-di-fan-zhuan">原地反转</a></li>
<li><a href="#gao-xiao-de-yuan-di-fan-zhuan">高效的原地反转</a></li>
</ul>
</li>
<li><a href="#ce-shi-yan-zheng">测试验证</a><ul>
<li><a href="#gong-neng-ce-shi">功能测试</a></li>
<li><a href="#sui-ji-ce-shi">随机测试</a></li>
<li><a href="#ji-zhun-ce-shi">基准测试</a></li>
</ul>
</li>
</ul>
</div>
<h3 id="fan-zhuan-utf-8-bian-ma-de-zi-fu-chuan"><a class="toclink" href="#fan-zhuan-utf-8-bian-ma-de-zi-fu-chuan">反转 UTF-8 编码的字符串</a></h3>
<p>实现一个 reverse 函数，反转 UTF-8 编码的字符串中的字符元素。传入的参数是字符串对应的字节切片类型（[]byte）。  </p>
<h4 id="jian-dan-de-shi-xian"><a class="toclink" href="#jian-dan-de-shi-xian">简单的实现</a></h4>
<p>首先，不考虑效率，先用一个简单的逻辑来实现。切片的反转方法如下：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">reverse</span><span class="p">(</span><span class="nx">s</span><span class="w"> </span><span class="p">[]</span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">j</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="o">-</span><span class="mi">1</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">s</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span><span class="w"> </span><span class="nx">s</span><span class="p">[</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">s</span><span class="p">[</span><span class="nx">j</span><span class="p">],</span><span class="w"> </span><span class="nx">s</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>只要将数据转成字符切片，然后套用切片反转的代码就可以了：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// 先转成字符切片，然后再用切片的反转的方法，最后更新参数指向的底层数组</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="nx">reverse_rune</span><span class="p">(</span><span class="nx">slice</span><span class="w"> </span><span class="p">[]</span><span class="kt">byte</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">r</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">[]</span><span class="nb">rune</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">slice</span><span class="p">))</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">r</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">j</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="o">-</span><span class="mi">1</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">r</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="nx">j</span><span class="p">],</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">slice</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">slice</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">r</span><span class="p">))[</span><span class="nx">i</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>这个方法逻辑清晰，适合之后拿来做随机测试。  </p>
<h4 id="yuan-di-fan-zhuan"><a class="toclink" href="#yuan-di-fan-zhuan">原地反转</a></h4>
<p>下面的函数实现了原地反转。每次读取第一个字符，把尾部标志位之前的一串字符移到开头，再把之前读的字符放到标志位的位置，然后向前移动标志位：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">reverse_byte</span><span class="p">(</span><span class="nx">slice</span><span class="w"> </span><span class="p">[]</span><span class="kt">byte</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">l</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">slice</span><span class="p">);</span><span class="w"> </span><span class="nx">l</span><span class="w"> </span><span class="p">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">r</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">utf8</span><span class="p">.</span><span class="nx">DecodeRuneInString</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">slice</span><span class="p">[</span><span class="mi">0</span><span class="p">:]))</span><span class="w"></span>
<span class="w">        </span><span class="nb">copy</span><span class="p">(</span><span class="nx">slice</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="nx">l</span><span class="p">],</span><span class="w"> </span><span class="nx">slice</span><span class="p">[</span><span class="mi">0</span><span class="o">+</span><span class="nx">size</span><span class="p">:</span><span class="nx">l</span><span class="p">])</span><span class="w"></span>
<span class="w">        </span><span class="nb">copy</span><span class="p">(</span><span class="nx">slice</span><span class="p">[</span><span class="nx">l</span><span class="o">-</span><span class="nx">size</span><span class="p">:</span><span class="nx">l</span><span class="p">],</span><span class="w"> </span><span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">r</span><span class="p">)))</span><span class="w"></span>
<span class="w">        </span><span class="nx">l</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="nx">size</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>这个实现效率还是稍差，之后会做测试比较。  </p>
<h4 id="gao-xiao-de-yuan-di-fan-zhuan"><a class="toclink" href="#gao-xiao-de-yuan-di-fan-zhuan">高效的原地反转</a></h4>
<p>下面的函数，用了很多的标志位，实现了一个高效的原地反转：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">reverse</span><span class="p">(</span><span class="nx">s</span><span class="w"> </span><span class="p">[]</span><span class="kt">byte</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="nx">lRd</span><span class="p">,</span><span class="w"> </span><span class="nx">rRd</span><span class="w">           </span><span class="kt">int</span><span class="w">  </span><span class="c1">// 读指针</span><span class="w"></span>
<span class="w">        </span><span class="nx">lWr</span><span class="p">,</span><span class="w"> </span><span class="nx">rWr</span><span class="w">           </span><span class="kt">int</span><span class="w">  </span><span class="c1">// 写指针</span><span class="w"></span>
<span class="w">        </span><span class="nx">lHasRune</span><span class="p">,</span><span class="w"> </span><span class="nx">rHasRune</span><span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="c1">// 是否有字符</span><span class="w"></span>
<span class="w">        </span><span class="nx">lr</span><span class="p">,</span><span class="w"> </span><span class="nx">rr</span><span class="w">             </span><span class="kt">rune</span><span class="w"> </span><span class="c1">// 读取到的字符</span><span class="w"></span>
<span class="w">        </span><span class="nx">lsize</span><span class="p">,</span><span class="w"> </span><span class="nx">rsize</span><span class="w">       </span><span class="kt">int</span><span class="w">  </span><span class="c1">// 读取到字符的宽度</span><span class="w"></span>
<span class="w">    </span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="nx">rRd</span><span class="p">,</span><span class="w"> </span><span class="nx">rWr</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">s</span><span class="p">),</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">lRd</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">rRd</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">lHasRune</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">lr</span><span class="p">,</span><span class="w"> </span><span class="nx">lsize</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">utf8</span><span class="p">.</span><span class="nx">DecodeRune</span><span class="p">(</span><span class="nx">s</span><span class="p">[</span><span class="nx">lRd</span><span class="p">:])</span><span class="w"></span>
<span class="w">            </span><span class="nx">lRd</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nx">lsize</span><span class="w"></span>
<span class="w">            </span><span class="nx">lHasRune</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="kc">true</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">rHasRune</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">rr</span><span class="p">,</span><span class="w"> </span><span class="nx">rsize</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">utf8</span><span class="p">.</span><span class="nx">DecodeLastRune</span><span class="p">(</span><span class="nx">s</span><span class="p">[:</span><span class="nx">rRd</span><span class="p">])</span><span class="w"></span>
<span class="w">            </span><span class="nx">rRd</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="nx">rsize</span><span class="w"></span>
<span class="w">            </span><span class="nx">rHasRune</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="kc">true</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>

<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">lsize</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="nx">rWr</span><span class="o">-</span><span class="nx">rRd</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">utf8</span><span class="p">.</span><span class="nx">EncodeRune</span><span class="p">(</span><span class="nx">s</span><span class="p">[</span><span class="nx">rWr</span><span class="o">-</span><span class="nx">lsize</span><span class="p">:],</span><span class="w"> </span><span class="nx">lr</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="nx">rWr</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="nx">lsize</span><span class="w"></span>
<span class="w">            </span><span class="nx">lHasRune</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="kc">false</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">rsize</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="nx">lRd</span><span class="o">-</span><span class="nx">lWr</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">utf8</span><span class="p">.</span><span class="nx">EncodeRune</span><span class="p">(</span><span class="nx">s</span><span class="p">[</span><span class="nx">lWr</span><span class="p">:],</span><span class="w"> </span><span class="nx">rr</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="nx">lWr</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nx">rsize</span><span class="w"></span>
<span class="w">            </span><span class="nx">rHasRune</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="kc">false</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="c1">// 最后还可能会剩个字符没写</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">lHasRune</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">utf8</span><span class="p">.</span><span class="nx">EncodeRune</span><span class="p">(</span><span class="nx">s</span><span class="p">[</span><span class="nx">rWr</span><span class="o">-</span><span class="nx">lsize</span><span class="p">:],</span><span class="w"> </span><span class="nx">lr</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">rHasRune</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">utf8</span><span class="p">.</span><span class="nx">EncodeRune</span><span class="p">(</span><span class="nx">s</span><span class="p">[</span><span class="nx">lWr</span><span class="p">:],</span><span class="w"> </span><span class="nx">rr</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<h3 id="ce-shi-yan-zheng"><a class="toclink" href="#ce-shi-yan-zheng">测试验证</a></h3>
<p>下面是测试代码，来验证上面的函数的正确性以及效率。  </p>
<h4 id="gong-neng-ce-shi"><a class="toclink" href="#gong-neng-ce-shi">功能测试</a></h4>
<p>基于表的测试很直观也很简单，可以方便的添加更多测试用例：</p>
<div class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">tests</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[]</span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">input</span><span class="w"> </span><span class="kt">string</span><span class="w"></span>
<span class="w">    </span><span class="nx">want</span><span class="w"> </span><span class="kt">string</span><span class="w"></span>
<span class="p">}{</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="s">&quot;abc&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;cba&quot;</span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="s">&quot;123&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;321&quot;</span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="s">&quot;你好，世界!&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;!界世，好你&quot;</span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="s">&quot;a一二三,四五.六,z&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;z,六.五四,三二一a&quot;</span><span class="p">},</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">TestReverse_rune</span><span class="p">(</span><span class="nx">t</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">tests</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">s</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">test</span><span class="p">.</span><span class="nx">input</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">reverse_rune</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nx">test</span><span class="p">.</span><span class="nx">want</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">t</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">&quot;reverse(%q) = %q, want %q\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="p">.</span><span class="nx">input</span><span class="p">,</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s</span><span class="p">),</span><span class="w"> </span><span class="nx">test</span><span class="p">.</span><span class="nx">want</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">TestReverse_byte</span><span class="p">(</span><span class="nx">t</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">tests</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">s</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">test</span><span class="p">.</span><span class="nx">input</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">reverse_byte</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nx">test</span><span class="p">.</span><span class="nx">want</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">t</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">&quot;reverse(%q) = %q, want %q\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="p">.</span><span class="nx">input</span><span class="p">,</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s</span><span class="p">),</span><span class="w"> </span><span class="nx">test</span><span class="p">.</span><span class="nx">want</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">TestReverse</span><span class="p">(</span><span class="nx">t</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">tests</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">s</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">test</span><span class="p">.</span><span class="nx">input</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">reverse</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nx">test</span><span class="p">.</span><span class="nx">want</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">t</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">&quot;reverse(%q) = %q, want %q\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="p">.</span><span class="nx">input</span><span class="p">,</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s</span><span class="p">),</span><span class="w"> </span><span class="nx">test</span><span class="p">.</span><span class="nx">want</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>测试结果：</p>
<div class="highlight"><pre><span></span><code>PS H:\Go\src\gopl\exercise4\e7&gt; go test -run TestReverse -v
=== RUN   TestReverse_rune
--- PASS: TestReverse_rune (0.00s)
=== RUN   TestReverse_byte
--- PASS: TestReverse_byte (0.00s)
=== RUN   TestReverse
--- PASS: TestReverse (0.00s)
PASS
ok      gopl/exercise4/e7       0.263s
PS H:\Go\src\gopl\exercise4\e7&gt;
</code></pre></div>

<h4 id="sui-ji-ce-shi"><a class="toclink" href="#sui-ji-ce-shi">随机测试</a></h4>
<p>随机测试也是功能测试的一种，通过构建随机输入来扩展测试的覆盖范围。有两种策略：</p>
<ul>
<li>额外写一个函数，这个函数使用低效但是清晰的算法，然后检查两种实现的输出是否一致</li>
<li>构建符合某种模式的输入，这样就可以知道对应的输出。</li>
</ul>
<p>下面就是用第一种策略写的随机测试。为了让输出的内容有更好的可读性，选择了一些熟悉的字符生成随机字符：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// randomSte 返回一个随机字符串，它的长度和内容都是随机生成的</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="nx">randomStr</span><span class="p">(</span><span class="nx">rng</span><span class="w"> </span><span class="o">*</span><span class="nx">rand</span><span class="p">.</span><span class="nx">Rand</span><span class="p">)</span><span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">n</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">rng</span><span class="p">.</span><span class="nx">Intn</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span><span class="w"> </span><span class="c1">// 随机字符串最大长度24</span><span class="w"></span>
<span class="w">    </span><span class="nx">runes</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="kt">rune</span><span class="p">,</span><span class="w"> </span><span class="nx">n</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">var</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="kt">rune</span><span class="w"></span>
<span class="w">        </span><span class="k">switch</span><span class="w"> </span><span class="nb">rune</span><span class="p">(</span><span class="nx">rng</span><span class="p">.</span><span class="nx">Intn</span><span class="p">(</span><span class="mi">6</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="mi">0</span><span class="p">:</span><span class="w"> </span><span class="c1">// ASCII 字母，1个字节</span><span class="w"></span>
<span class="w">            </span><span class="nx">r</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">rune</span><span class="p">(</span><span class="nx">rng</span><span class="p">.</span><span class="nx">Intn</span><span class="p">(</span><span class="mh">0x4B</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mh">0x30</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="mi">1</span><span class="p">:</span><span class="w"> </span><span class="c1">// 希腊字母，2个字节</span><span class="w"></span>
<span class="w">            </span><span class="nx">r</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">rune</span><span class="p">(</span><span class="nx">rng</span><span class="p">.</span><span class="nx">Intn</span><span class="p">(</span><span class="mi">57</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mh">0x391</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="mi">2</span><span class="p">:</span><span class="w"> </span><span class="c1">// 日文</span><span class="w"></span>
<span class="w">            </span><span class="nx">r</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">rune</span><span class="p">(</span><span class="nx">rng</span><span class="p">.</span><span class="nx">Intn</span><span class="p">(</span><span class="mh">0xBF</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mh">0x3041</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="mi">3</span><span class="p">:</span><span class="w"> </span><span class="c1">// 韩文</span><span class="w"></span>
<span class="w">            </span><span class="nx">r</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">rune</span><span class="p">(</span><span class="nx">rng</span><span class="p">.</span><span class="nx">Intn</span><span class="p">(</span><span class="mh">0x2BA4</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mh">0xAC00</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">case</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">:</span><span class="w"> </span><span class="c1">// 中文</span><span class="w"></span>
<span class="w">            </span><span class="nx">r</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">rune</span><span class="p">(</span><span class="nx">rng</span><span class="p">.</span><span class="nx">Intn</span><span class="p">(</span><span class="mh">0x4E00</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mh">0x51D6</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="nx">runes</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">r</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">runes</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">TestRandomReverse</span><span class="p">(</span><span class="nx">t</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">seed</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">().</span><span class="nx">UTC</span><span class="p">().</span><span class="nx">UnixNano</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="nx">t</span><span class="p">.</span><span class="nx">Logf</span><span class="p">(</span><span class="s">&quot;Random seed: %d&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">seed</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="nx">rng</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">rand</span><span class="p">.</span><span class="nx">New</span><span class="p">(</span><span class="nx">rand</span><span class="p">.</span><span class="nx">NewSource</span><span class="p">(</span><span class="nx">seed</span><span class="p">))</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="mi">1000</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">test</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">randomStr</span><span class="p">(</span><span class="nx">rng</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">s1</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">test</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">reverse_rune</span><span class="p">(</span><span class="nx">s1</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">t</span><span class="p">.</span><span class="nx">Logf</span><span class="p">(</span><span class="s">&quot;%s =&gt; %s\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="p">,</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s1</span><span class="p">))</span><span class="w"></span>

<span class="w">        </span><span class="nx">s2</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">test</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">reverse_byte</span><span class="p">(</span><span class="nx">s2</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s1</span><span class="p">)</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">t</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">&quot;reverse_byte(%q) = %q, want %q\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="p">,</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s2</span><span class="p">),</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s1</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>

<span class="w">        </span><span class="nx">s3</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">test</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">reverse</span><span class="p">(</span><span class="nx">s3</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s1</span><span class="p">)</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s3</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">t</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">&quot;reverse_byte(%q) = %q, want %q\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="p">,</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s3</span><span class="p">),</span><span class="w"> </span><span class="nb">string</span><span class="p">(</span><span class="nx">s1</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>测试结果：</p>
<div class="highlight"><pre><span></span><code>PS H:\Go\src\gopl\exercise4\e7&gt; go test -run Random
PASS
ok      gopl/exercise4/e7       0.298s
PS H:\Go\src\gopl\exercise4\e7&gt;
</code></pre></div>

<p>还可以加上 -v 参数，查看详细的测试日志。  </p>
<h4 id="ji-zhun-ce-shi"><a class="toclink" href="#ji-zhun-ce-shi">基准测试</a></h4>
<p>基准测试也没什么特别的，把功能测试的测试用例全部跑一遍：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">BenchmarkReverse</span><span class="p">(</span><span class="nx">b</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">B</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">b</span><span class="p">.</span><span class="nx">N</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">tests</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">reverse</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">test</span><span class="p">.</span><span class="nx">input</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">BenchmarkReverse_rune</span><span class="p">(</span><span class="nx">b</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">B</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">b</span><span class="p">.</span><span class="nx">N</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">tests</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">reverse_rune</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">test</span><span class="p">.</span><span class="nx">input</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w">   </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">BenchmarkReverse_byte</span><span class="p">(</span><span class="nx">b</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">B</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">b</span><span class="p">.</span><span class="nx">N</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">test</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">tests</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">reverse_byte</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">test</span><span class="p">.</span><span class="nx">input</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w">   </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>测试结果：</p>
<div class="highlight"><pre><span></span><code>PS H:\Go\src\gopl\exercise4\e7&gt; go test -benchmem -bench .
goos: windows
goarch: amd64
pkg: gopl/exercise4/e7
BenchmarkReverse-8               5000000               286 ns/op               0 B/op          0 allocs/op
BenchmarkReverse_rune-8           500000              3610 ns/op               0 B/op          0 allocs/op
BenchmarkReverse_byte-8          3000000               583 ns/op               0 B/op          0 allocs/op
PASS
ok      gopl/exercise4/e7       6.226s
PS H:\Go\src\gopl\exercise4\e7&gt;
</code></pre></div>
      </div>
      <div class="back-to-top">
        <a href="/">HOME</a>
        <a href="#top">TOP</a>
      </div>
    </article>
  </div>
<!-- end article -->
<!-- 页面往下滚动一段之后才会显示TOC -->
<script>
  window.onscroll = function() {
    var tocbox = document.getElementsByClassName('toc')[0];
    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
    var osWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
    // console.log(osTop)
    if (osTop>300 && osWidth>865) {
      tocbox.style.display = "block"
    }
    if (osTop<300 || osWidth<865) {
      tocbox.style.display = "none"
    }
  }
</script>
                <footer>
                    <div class="icons">
                    </div>
                    <span id="busuanzi_container_page_pv" style="padding: 10px">本文阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                    <span id="busuanzi_container_site_pv" style="padding: 10px">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
                    <span id="busuanzi_container_site_uv" style="padding: 10px">本站总访客数<span id="busuanzi_value_site_uv"></span>人</span>
                    <p>© <script>document.write(moment().format('YYYY'));</script> 749B</p>
                </footer>
        </div>
</body>
</html>